/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * ServerConfig.java
 *
 * Created on Aug 27, 2011, 4:26:34 PM
 */
package view;

import control.PropertiesUtility;
import javax.swing.JOptionPane;

/**
 *
 * @author TranDang
 */
public class ServerConfig extends javax.swing.JDialog {

    /** Creates new form ServerConfig */
    public ServerConfig(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        loadValues();
        this.setLocationRelativeTo(null);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jtfServer = new javax.swing.JTextField();
        jtfPassword = new javax.swing.JTextField();
        password = new javax.swing.JLabel();
        jbCancel = new javax.swing.JButton();
        jtfDBName = new javax.swing.JTextField();
        jbSave = new javax.swing.JButton();
        DBName = new javax.swing.JLabel();
        configIcon = new javax.swing.JLabel();
        userName = new javax.swing.JLabel();
        server = new javax.swing.JLabel();
        port = new javax.swing.JLabel();
        caption = new javax.swing.JLabel();
        jtfUserName = new javax.swing.JTextField();
        jtfPort = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Server Config");

        password.setFont(new java.awt.Font("Courier New", 0, 14));
        password.setText("Password:");

        jbCancel.setText("Cancel");
        jbCancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbCancelActionPerformed(evt);
            }
        });

        jbSave.setText("Save");
        jbSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jbSaveActionPerformed(evt);
            }
        });

        DBName.setFont(new java.awt.Font("Courier New", 0, 14));
        DBName.setText("Database:");

        configIcon.setIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/configuration.png"))); // NOI18N

        userName.setFont(new java.awt.Font("Courier New", 0, 14));
        userName.setText("User Name:");

        server.setFont(new java.awt.Font("Courier New", 0, 14));
        server.setText("Server:");

        port.setFont(new java.awt.Font("Courier New", 0, 14));
        port.setText("Port:");

        caption.setFont(new java.awt.Font("Courier New", 1, 18));
        caption.setText("Server Information:");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(caption)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(DBName)
                            .addComponent(server)
                            .addComponent(port)
                            .addComponent(userName)
                            .addComponent(password))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jtfPassword)
                            .addComponent(jtfUserName)
                            .addComponent(jtfPort)
                            .addComponent(jtfServer)
                            .addComponent(jtfDBName, javax.swing.GroupLayout.DEFAULT_SIZE, 187, Short.MAX_VALUE))
                        .addGap(18, 18, 18)
                        .addComponent(configIcon)))
                .addGap(24, 24, 24))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(301, Short.MAX_VALUE)
                .addComponent(jbSave)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jbCancel)
                .addGap(22, 22, 22))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(caption)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(server)
                            .addComponent(jtfServer, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(11, 11, 11)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(port)
                            .addComponent(jtfPort, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(DBName)
                            .addComponent(jtfDBName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(userName)
                            .addComponent(jtfUserName, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(password)
                            .addComponent(jtfPassword, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(configIcon))
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jbSave)
                    .addComponent(jbCancel))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jbCancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbCancelActionPerformed
    this.dispose();
}//GEN-LAST:event_jbCancelActionPerformed

private void jbSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jbSaveActionPerformed
    String[] values = new String[5];
    values[0] = jtfServer.getText();
    values[1] = jtfPort.getText();
    values[2] = jtfDBName.getText();
    values[3] = jtfUserName.getText();
    values[4] = jtfPassword.getText();
    if(isComponentValid(values)){
        PropertiesUtility.createDBProperties(values);
        this.dispose();
    }
}//GEN-LAST:event_jbSaveActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel DBName;
    private javax.swing.JLabel caption;
    private javax.swing.JLabel configIcon;
    private javax.swing.JButton jbCancel;
    private javax.swing.JButton jbSave;
    private javax.swing.JTextField jtfDBName;
    private javax.swing.JTextField jtfPassword;
    private javax.swing.JTextField jtfPort;
    private javax.swing.JTextField jtfServer;
    private javax.swing.JTextField jtfUserName;
    private javax.swing.JLabel password;
    private javax.swing.JLabel port;
    private javax.swing.JLabel server;
    private javax.swing.JLabel userName;
    // End of variables declaration//GEN-END:variables

    private void loadValues() {
        String[] values = PropertiesUtility.getDBProperties();
        jtfServer.setText(values[0]);
        jtfPort.setText(values[1]);
        jtfDBName.setText(values[2]);
        jtfUserName.setText(values[3]);
        jtfPassword.setText(values[4]);
    }

    private boolean isComponentValid(String[] values) {
        if (values[0] == null||values[0].isEmpty()) {
            JOptionPane.showMessageDialog(this, "Enter your server name or IP", "Error", JOptionPane.ERROR_MESSAGE);
            return false;
        } else if (values[1] == null||values[1].isEmpty()) {
            JOptionPane.showMessageDialog(this, "Enter your server port", "Error", JOptionPane.ERROR_MESSAGE);
            return false;
        } else if (values[2] == null||values[2].isEmpty()) {
            JOptionPane.showMessageDialog(this, "Enter your database name", "Error", JOptionPane.ERROR_MESSAGE);
            return false;
        } else if (values[3] == null||values[3].isEmpty()) {
            JOptionPane.showMessageDialog(this, "Enter your username", "Error", JOptionPane.ERROR_MESSAGE);
            return false;
        } else if (values[4] == null||values[4].isEmpty()) {
            JOptionPane.showMessageDialog(this, "Enter your password", "Error", JOptionPane.ERROR_MESSAGE);
            return false;
        } else {
            return true;
        }
    }
}
